package com.kitty.game.i18n;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.Reloadable;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class I18nIdDataPool implements Reloadable {

    public static Map<Integer, P_I18nId> ids ;

    @Override
    public void reload() {
        Dao dao = SpringUtils.getBean(Dao.class);
        List<P_I18nId> list = dao.query(P_I18nId.class, Cnd.NEW());
        Map<Integer, P_I18nId> map = new HashMap<>(list.size());

        for (P_I18nId id : list) {
            map.put(id.getId(), id);
        }

        I18nIdDataPool.ids = map;
    }

    /**
     * 根据i18nId获得 内容
     * @param i18nId
     * @return
     */
    public static String getI18nContent(int i18nId) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            return idResource.getContent();
        }

        return null;
    }

    /**
     * 根据i18nId获得 内容
     * @param i18nId
     * @param args
     * @return
     */
    public static String getI18nContent(int i18nId,  Object... args) {
        P_I18nId idResource = I18nIdDataPool.ids.get(i18nId);
        if (idResource != null) {
            return String.format(idResource.getContent(), args);
        }

        return null;
    }
}
